<?php
namespace App\Services;

use App\Lib\Code;
use App\Repositories\Contracts\RemitOrderRepository;
use App\Repositories\Criteria\RemitSettingCriteria;
use App\Repositories\Eloquent\PlatSettingRepository;
use Illuminate\Http\Request;

class RemitOrderService
{
    protected $remitOrderRepository;
    protected $settingRepository;

    public function __construct(PlatSettingRepository $settingRepository)
    {
        $this->settingRepository = $settingRepository;
    }

    /**
     * @param        $money
     * @param array  $setting
     * @param string $classify plat|api
     *
     * @return float $fee
     * @internal param $setting
     */
    public function remitFee($money, $setting, $classify = 'plat')
    {
        $this->settingRepository->pushCriteria(RemitSettingCriteria::class);
        $fee = 0;
        switch ($classify) {
            case 'plat':
                $fee = $this->getPlatFee($money, $setting);
                break;
            case 'api':
                $fee = $this->getApiFee($money, $setting);
                break;
        }
        return $fee;

    }

    public function isPassable($money, $setting, $classify='plat')
    {
        if ($classify == 'plat') {
            if (bccomp($money, $setting['single_max'], 2) == 1) {
                return Code::WITHDRAW_TOO_HIGH;
            }
            if (bccomp($money, $setting['single_min'], 2) == -1) {
                return Code::WITHDRAW_TOO_LOW;
            }
        }
        if ($classify == 'api') {
            if (bccomp($money, $setting['single_max_api'], 2) == 1) {
                return Code::WITHDRAW_TOO_HIGH;
            }
            if (bccomp($money, $setting['single_min_api'], 2) == -1) {
                return Code::WITHDRAW_TOO_LOW;
            }
        }
        return true;
    }

    /**
     * @param \App\Models\PlatUser $user
     * @param $setting
     *
     * @return bool
     */
    public function isRemitAble($user, $setting)
    {
        $asset = $user->assets;
        if (bccomp($asset->available, $setting['threshold'], 2) == -1) {
            return false;
        }
        return true;
    }

    protected function getPlatFee($money, $setting)
    {
        if ($setting['fee_type'] == 0) {
            return $setting['fee_fixed'];
        } else {
            $fee = bcmul($money, bcdiv($setting['fee_rate'], 100, 4), 2);
            if (bccomp($fee, $setting['fee_max'], 2) == 1) {
                return $setting['fee_max'];
            }
            if (bccomp($fee, $setting['fee_min'], 2) == -1) {
                return $setting['fee_min'];
            }
            return $fee;
        }
    }

    protected function getApiFee($money, $setting)
    {
        if ($setting['fee_type_api'] == 0) {
            return $setting['fee_fixed_api'];
        } else {
            $fee = bcmul($money, bcdiv($setting['fee_rate_api'], 100, 4), 2);
            if (bccomp($fee, $setting['fee_max_api'], 2) == 1) {
                return $setting['fee_max_api'];
            }
            if (bccomp($fee, $setting['fee_min_api'], 2) == -1) {
                return $setting['fee_min_api'];
            }
            return $fee;
        }
    }
}
